package com.iwith.dongmoon.util;

import java.io.IOException;

import java.net.HttpURLConnection;
import java.net.URL;

import android.app.Activity;
import android.content.SharedPreferences;

public class ConnectionManager {

	// connect()���� ���� ���ؼ��� ����
	public static HttpURLConnection getConnection(Activity activity, URL url)
	{
		SharedPreferences pref = activity.getSharedPreferences("com.apposter.toksong", Activity.MODE_PRIVATE);
		String cookie = "";
		String sessionId = pref.getString("JSESSIONID", "");
		String accessCode = pref.getString("ACCESS-CODE", "");

		if (sessionId.equals("") == false)
		{
			cookie += sessionId;
		}
		
		if (accessCode.equals("") == false)
		{
			cookie += ";" + accessCode;
		}
		
		HttpURLConnection connection = null;
		try {
			connection = (HttpURLConnection)url.openConnection();
			
			if(cookie.length() > 0) {
				connection.setRequestProperty("cookie", cookie);
			}
			
		} catch (IOException e) {
			e.printStackTrace();
		}
		
		return connection;
	}
	
	
	// connect() �Ŀ� ȣ���Ͽ� cookie�� SharedPreferences�� ����
	public static void setCookie(Activity activity, HttpURLConnection connection)
	{
		SharedPreferences pref = activity.getSharedPreferences("com.apposter.toksong", Activity.MODE_PRIVATE);
		String cookie = connection.getHeaderField("Set-Cookie");
		
		if (cookie != null) {
			SharedPreferences.Editor editor = pref.edit();
			editor.putString("cookie", cookie);
			editor.commit();
		}
	}
}
